﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using planerka.DataAccess.Models;
using System.Web.UI;
using System.Web.Mvc;
using planerka.DataAccess;
using planerka.DataAccess.DataService;

namespace planerka.Web.Helpers
{
    public static class TreeViewHelper
    {

        
        //db.ObjectTrackingEnabled = false;

        ////////////////////////////////////////////////////////////////////////////

        public static string renderPageCategories(this HtmlHelper html, int selected_ID)
        {
            //HtmlTextWriter writer = new HtmlTextWriter(html.ViewContext.HttpContext.Response.Output);
            string retString = "";
            PageService pageService = new PageService();
            //db.ObjectTrackingEnabled = false;
            List<pageCategory> PCs = new _Repository<pageCategory>().All().OrderBy(p => p.order).ToList();
            // Передадим пустой список страниц
            List<page.pageNavs> pages = new _Repository<page>().All().Where(p => p.ID == -1).Select(p => new page.pageNavs { id = p.ID, header = p.header, category_ID = p.category_ID }).ToList();
            string contextPath = "/" + System.Configuration.ConfigurationManager.AppSettings["adminDirectory"] + 
                "/managePageCategories/"; 
            int? parent_ID = null;
            retString += (@"<ul id=""browser"" class=""filetree treeview"">");
            retString = returnLevelCategories(
                retString, 
                parent_ID, 
                PCs, pages, 
                selected_ID, 
                0, // не может быть выбранной страницы
                contextPath,
                false);
            retString += ("</ul>");

            return retString;
        }

        //

        private static string returnLevelCategories(
            //HtmlTextWriter writer, 
            string retString,
            int? parent_ID, 
            List<pageCategory> PCs, 
            List<page.pageNavs> pages, 
            int selectedCat_ID,
            int selectedPage_ID,
            string contextPath,
            bool showPages)
        {
            List<pageCategory> levelPCs = PCs.Where(p => p.parent_ID == parent_ID).ToList();
            foreach (var levelPC_item in levelPCs)
            {

                //writer.RenderBeginTag(HtmlTextWriterTag.Li);
                retString += "<li>";
                retString += (@"<span class=""folder"">");
                if (levelPC_item.ID == selectedCat_ID)
                    retString += (@"<strong><b>");
                    
    

                // Если showPages - тогда папки без ссылок
                if (!showPages)
                    retString += (@"<a href=""" + contextPath + levelPC_item.ID + @""">");
                
                retString += (levelPC_item.name);
                
                if (levelPC_item.ID == selectedCat_ID)
                    retString += ("</b></strong>"); 
   
                if (!showPages)
                    retString += ("</a>");

                retString += ("</span>");

                

                List<pageCategory> subPCs = PCs.Where(p => p.parent_ID == levelPC_item.ID).ToList();
                List<page.pageNavs> subPages = pages.Where(p => p.category_ID == levelPC_item.ID).ToList();

                if ((subPages.Count + subPCs.Count) > 0)
                {
                    retString += ("<ul>");
                    retString = returnLevelCategories(retString, levelPC_item.ID, PCs, pages, selectedCat_ID, selectedPage_ID, contextPath, showPages);
                    foreach (var subPage in subPages)
                    {
                        retString += "<li>";
                        retString += (@"<span class=""file"">");
                        if (subPage.id == selectedPage_ID)
                            retString += ("<strong><b>");

                        retString += (@"<a href=""" + contextPath + subPage.id + @""">" + subPage.header + "</a></span>");
                        if (subPage.id == selectedPage_ID)
                            retString += ("</b></strong>");

                        retString += "</li>";
                    }
                    retString += ("</ul>");
                }

                retString += "</li>";
            }

            return retString;
        }

        ////////////////////////////////////////////////////////////////////////////

        public static string renderPagesAndPageCategories(this HtmlHelper html, int selectedPage_ID)
        {
          
            string retString = "";

            PageService pageService = new PageService();
            List<pageCategory> PCs = new _Repository<pageCategory>().All().OrderBy(p => p.order).ToList();
            string contextPath = "/" + System.Configuration.ConfigurationManager.AppSettings["adminDirectory"] + 
                "/managePages/";
            int? parent_ID = null;
            
            retString += (@"<ul id=""browser"" class=""filetree treeview"">");
            List<page.pageNavs> pages = new _Repository<page>().All().OrderBy(p => p.order).Select(p => new page.pageNavs { id = p.ID, header = p.header, category_ID = p.category_ID }).ToList();
            retString += returnLevelCategories(
                retString, 
                parent_ID, 
                PCs, 
                pages, 
                0, // нет выбранной категории
                selectedPage_ID, 
                contextPath, 
                true);
      
            retString += ("</ul>");
            return retString;

        }
    }
}